import QtQuick
import QtQuick.Controls
import Qt.labs.platform

ApplicationWindow {
    id: root
    width: 1080
    height: 640
    minimumWidth: 900
    minimumHeight: 540
    visible: true
    flags: Qt.Window | Qt.FramelessWindowHint | Qt.WindowSystemMenuHint | Qt.WindowMinMaxButtonsHint

    Component.onCompleted: {
//        console.log(Screen.width +" "+ Screen.height)
        // 默认最大化窗口
        customHeaderBar.isMaximized = true
        customHeaderBar.setMaximizeOrRestore(customHeaderBar.isMaximized)
    }
    // 配置灯效的必要参数
    function ledEditViewConfig() {
        ledEditView.setDisplayLedNumX(myHandle.getMyProperty("lNum")) // 设置灯珠数
        ledEditView.setDisplayLedNumY(1)
        ledEditView.setTimerInterval(1000 / Number(myHandle.getMyProperty("fRate"))) // 设置定时器周期
        ledEditView.setValidFrameNum(myHandle.getMyProperty("fValidNum")) // 设置有效帧数
    }

    Connections {
        target: myHandle
        // 链接C++的提示信息显示需求
        function onTipsShow(tips) {
            tipsTextRect.showTips(qsTr(tips))
        }
    }

    // 托盘 ApplicationWindow可用
    SystemTrayIcon {
        visible: true
        icon.source: "qrc:/imgs/ledEditTool.png"

        onActivated: {
        }

        menu: Menu {
            id: menu
            MenuItem {
                text: qsTr("显示主界面")
                onTriggered: root.show()
            }
            MenuSeparator {}
            MenuItem {
                text: qsTr("退出")
                onTriggered: Qt.quit()
            }
         }
    }

    // 遮罩层
    Rectangle {
        id: shadeRect
        width: parent.width
        height: parent.height
        color: "#e0e1e2"
        visible: false
        opacity: 0.4
        z: 99

        // 屏蔽部分鼠标操作
        MouseArea {
            anchors.fill: parent
            hoverEnabled: true
            propagateComposedEvents: false
            acceptedButtons: Qt.LeftButton | Qt.RightButton
            onWheel: {}
            onEntered: {}
            onExited: {}
        }
    }

    // 视频导入预览页面
    ImportVideoWin {
        id: importVideoWin
        x: (parent.width - width) / 2
        y: (parent.height - height) / 2
        width: 640
        height: 480
        visible: false
        z: 100

        onVisibleChanged: {
            shadeRect.visible = visible
        }

        // 视频文件处理完毕信号
        onVideoFileProcessOver: {
            ledEditViewConfig() // 设置LED编辑显示区
            visible = false // 关闭页面
            tipsTextRect.showTips(qsTr("视频导入完成!"))
        }

        onActionErrorTips: function(tips) {
            tipsTextRect.showTips(qsTr(tips))
        }
    }

    ImportLDFileWin {
        id: importLDFileWin
        x: (parent.width - width) / 2
        y: (parent.height - height) / 2
        width: 640
        height: 480
        visible: false
        z: 100

        onVisibleChanged: {
            shadeRect.visible = visible
        }

        // ld文件处理完毕信号
        onLdFileProcessOver: {
            ledEditViewConfig() // 设置LED编辑显示区
            visible = false // 关闭页面
            tipsTextRect.showTips(qsTr("ld文件导入完成!"))
        }

        onActionErrorTips: function(tips) {
            tipsTextRect.showTips(qsTr(tips))
        }
    }

    ImportFramesWin {
        id: importFramesWin
        x: (parent.width - width) / 2
        y: (parent.height - height) / 2
        width: 640
        height: 480
        visible: false
        z: 100

        onVisibleChanged: {
            shadeRect.visible = visible
        }

        // ld文件处理完毕信号
        onFramesProcessOver: {
            ledEditViewConfig() // 设置LED编辑显示区
            visible = false // 关闭页面
            tipsTextRect.showTips(qsTr("帧图片导入完成!"))
        }

        onActionErrorTips: function(tips) {
            tipsTextRect.showTips(qsTr(tips))
        }
    }

    UploadLEDWin {
        id: uploadLEDWin
        x: (parent.width - width) / 2
        y: (parent.height - height) / 2
        width: 640
        height: 480
        visible: false
        z: 100

        onVisibleChanged: {
            shadeRect.visible = visible
        }

        onUploadEffectOver: {
            tipsTextRect.showTips(qsTr("更新灯效完成!"))
        }
    }

    // 帧率设置页面
    CustomBorderTitle {
        id: frameRateConfigRect
        x: (parent.width - width) / 2
        y: (parent.height - height) / 2
        width: 200
        height: 60
        visible: false
        z: 100
        radius: 10
        cbtTitle: qsTr("设置播放帧率")
        cbtFontSize: 14
        cbtTitleBackColor: "#34393e"
        cbtTitleColor: "#e1e4e8"
        cbtTitleX: 15
        color: "#34393e"

        onVisibleChanged: {
            shadeRect.visible = visible
            if(visible) frameRateConfig.inputText = myHandle.getMyProperty("fRate")
        }

        TitleTextInput {
            id: frameRateConfig
            x: 10
            y: 18
            width: 120
            height: 30
            titleText: qsTr("播放帧率:")
            titleWidth: 70
            textColor: "#e1e4e8"
            textSize: 14
            inputBackColor: "#34393e"
            inputBorderWidth: 1
            inputBorderColor: "#888888"
            inputText: "30"
        }

        AsButton {
            id: frameRateConfigBtn
            x: 140
            y: 18
            width: 50
            height: 30
            text: qsTr("确认")
            font.pixelSize: 14
            borderWidth: 1
            backColor: "#34393e"
            textColor: "#e1e4e8"
            itemRadius: 8

            onClicked: {
                myHandle.setMyProperty("fRate", frameRateConfig.inputText) // 设置播放帧率
                ledEditView.setTimerInterval(1000 / Number(myHandle.getMyProperty("fRate"))) // 设置定时器周期
                parent.visible = false
            }
        }
    }

    // 提示页面
    TipsTextWin {
        id: tipsTextRect
        anchors.right: parent.right
        anchors.rightMargin: 10
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 40
        width: 160
        height: 50
        visible: false
        radius: 10
        z: 100

        onFocusChanged: {
            visible = false
        }
    }

    // 裁剪和保留页面
    FrameCutWin {
        id: frameCutWin
        x: (parent.width - width) / 2
        y: (parent.height - height) / 2
        width: 640
        height: 480
        visible: false
        z: 100

        onVisibleChanged: {
            shadeRect.visible = visible
        }

        onFrameCutSaveTriggered: {
            ledEditViewConfig()
        }
    }

    UploadVersionWin {
        id: uploadVersionWin
        x: (parent.width - width) / 2
        y: (parent.height - height) / 2
        width: 640
        height: 480
        visible: false
        z: 100

        onVisibleChanged: {
            shadeRect.visible = visible
        }

        onUploadVersionOver: {
            tipsTextRect.showTips(qsTr("固件升级完成!"))
        }
    }

    AboutSoftwareWin {
        id: aboutSoftwareWin
        x: (parent.width - width) / 2
        y: (parent.height - height) / 2
        width: 640
        height: 480
        visible: false
        z: 100

        onVisibleChanged: {
            shadeRect.visible = visible
        }
    }

    UseSoftwareWin {
        id: useSoftwareWin
        x: (parent.width - width) / 2
        y: (parent.height - height) / 2
        width: 640
        height: 480
        visible: false
        z: 100

        onVisibleChanged: {
            shadeRect.visible = visible
        }
    }

    ResizeItem {
        id: resizeItem
        anchors.fill: parent
        enableSize: 8
        focus: true

        // 头部工具栏
        CustomHeaderBar {
            id: customHeaderBar
            anchors.top: parent.top
            width: parent.width
            height: 34
            color: "#1f2428"
            leftSideWidth: 200
            rightSideWidth: 150
            titleText: qsTr("LED Edit Tool V1.0")
            z: 10

            onSpinnerBtnFileClicked: function(index) {
                switch(index) {
                    case 0: // 导入视频文件
                        focus = true
                        ledEditView.videoFrameCount = 0 // 当前视频帧计数清零
                        ledEditView.videoTimerPlayPause(false) // 视频播放定时器停止
                        importVideoWin.visible = true // 视频文件导入预览页面
                        break;
                    case 1: // 导入帧图片
                        focus = true
                        importFramesWin.visible = true
                        break;
                    case 2: // 导入ld文件
                        focus = true
                        importLDFileWin.visible = true
                        break;
                    case 3: // 导出ld文件
                        focus = true
                        myHandle.createVideoFileLD() // 执行C++方法 创建LD文件
                        break;
                    case 4: // 退出
                        focus = true
                        Qt.quit()
                        break;
                    default: focus = true; break;
                }
            }

            onSpinnerBtnEditClicked: function(index) {
                switch(index) {
                    case 0: // 设置播放帧率
                        focus = true
                        frameRateConfigRect.visible = true
                        break;
                    case 1: // 裁剪和保留
                        focus = true
                        frameCutWin.visible = true
                        break;
                    default: focus = true; break;
                }
            }

            onSpinnerBtnDeviceClicked: function(index) {
                switch(index) {
                    case 0: // 在线导入灯效
                        focus = true
                        uploadLEDWin.visible = true
                        break;
                    case 1: // 固件升级
                        focus = true
                        uploadVersionWin.visible = true
                        break;
                    default: focus = true; break;
                }
            }

            onSpinnerBtnHelpClicked: function(index) {
                switch(index) {
                    case 0: // 使用说明
                        focus = true
                        useSoftwareWin.visible = true
                        break;
                    case 1: // 关于软件
                        focus = true
                        aboutSoftwareWin.visible = true
                        break;
                    default: focus = true; break;
                }
            }
        }

        // 左侧边栏
        LeftSideEditView {
            id: leftSideEditView
            anchors.top: customHeaderBar.bottom
            anchors.bottom: parent.bottom
            width: 0 // 240
            z: 1
            visible: false

            property bool ssss: false

            onConfigPlayBtnClicked: {
                ledEditViewConfig()
            }
        }

        // 灯光编辑区域
        LedEditView {
            id: ledEditView
            anchors.top: customHeaderBar.bottom
            anchors.bottom: parent.bottom
            anchors.left: leftSideEditView.right
            anchors.right: parent.right

            onRightBtnMenuClicked: function(menuName) {
                switch(menuName) {
                    case "playRate": frameRateConfigRect.visible = true; break;
                    case "frameCut": frameCutWin.visible = true; break;
                }
            }
        }
    }
}
